<?php


namespace app\index\controller;


use think\Db;
use think\facade\Config;

class Notice extends \Common {

    public function GetNoticeList(){
        $aid = $this->token["aid"];
        if(isset($_GET["page"]) && isset($_GET["limit"]) && isset($_GET["user_id"])){
            $page = $_GET["page"]; $limit = $_GET["limit"]; $user_id = $_GET["user_id"];
            $list = []; $where = []; if($user_id != ""){ $where["aid"] = $user_id; }
            $notion = Db::name("notion")->where($where)->order("is_import desc, releaseTime desc")->page($page, $limit)->select();
            $total = Db::name("notion")->where($where)->count();
            $admin = Db::name("admin")->field("aid, name")->select();
            $admin_arr = [];
            foreach ($admin as $val){
                $admin_arr[$val["aid"]] = $val["name"];
            }
            foreach ($notion as $value){
                $read = json_decode($value["unread"], true);
                $unreadStr = [];
                foreach ($admin_arr as $key => $val){
                    if(!in_array($key, $read)){
                        $unreadStr[] = $val;
                    }
                }
                $admin = Db::name("admin")->where("aid", $value["aid"])->find();

                $content1 = htmlspecialchars_decode($value["content"]);
                $content2 = str_replace("&nbsp;","",$content1);
                $content3 = strip_tags($content2);
                $content_sum = mb_substr($content3, 0, 20,"utf-8");



                $list[] = [
                    "id" => (int)$value["nid"],
                    "title" => $value["title"],
                    "content" => $value["content"],
                    "content_sum" => $content_sum,
                    "time" => (int)$value["releaseTime"] * 1000,
                    "important" => $value["is_import"] == 1,
                    "publisher" => empty($admin) ? "" : $admin["name"],
                    "unread" => $unreadStr,
                    "haveRead" => in_array($aid, $read),
                    "isMine" => $value["aid"] == $aid
                ];
            }
            $data = [
                "list" => $list,
                "total" => $total
            ];
            $this->Code(200, false, $data);
        }else{
            $this->Code(403, 403);
        }
    }

    public function UpdateNotice(){
        $aid = $this->token["aid"];
        if(isset($_POST["title"])){
            $info = [
                "title"       => $_POST["title"],
                "content"     => isset($_POST["content"]) ? $_POST["content"] : "",
                "releaseTime" => $_POST["time"] / 1000,
                "is_import"   => $_POST["important"] == "false" ? 0 : 1,
            ];
        }
        if(isset($_POST["id"])){ # 修改
            if(isset($_POST["id"]) && isset($_POST["important"]) && !isset($_POST["title"])){
                $import = [
                    "is_import"   => $_POST["important"] == "false" ? 0 : 1,
                ];
                Db::name("notion")->where("nid", $_POST["id"])->update($import);
                $this->Code(200, "修改成功");
            }else{
                Db::name("notion")->where("nid", $_POST["id"])->update($info);
                $this->Code(200, "修改成功");
            }
        }else{# 新增
            $info["aid"] = $aid; $info["unread"] = json_encode([$aid], 256);
            Db::name("notion")->insert($info);
            $this->Code(200, "添加成功");
        }
    }

    public function SetReadState(){
        $aid = $this->token["aid"];
        if(isset($_POST["id"])){
            $notice = Db::name("notion")->where("nid", $_POST["id"])->find();
            if(!empty($notice)){
                $arr = json_decode($notice["unread"], true);
                array_push($arr, $aid);
                $upInfo = [
                    "unread" => json_encode($arr, 256)
                ];
                Db::name("notion")->where("nid", $_POST["id"])->update($upInfo);
                $this->Code(200, false);
            }else{
                $this->Code(403, 403);
            }
        }else{
            $this->Code(403, 403);
        }
    }

    public function DelNotice(){
        if(isset($_POST["id"])){
            Db::name("notion")->where("nid", $_POST["id"])->delete();
            $this->Code(200, "删除成功");
        }else{
            $this->Code(403, 403);
        }
    }

    public function UploadImage(){
        $aid = $this->token["aid"];
        $return = uploadFile($_FILES["file"], "./static/upload/notion/", $aid.rand(0000, 9999).time());
        if($return){
            $this->Code(200, false, Config::get("ip")."static/upload/notion/{$return}");
        }else{
            $this->Code(403, 403);
        }
    }

    public function deleteImages(){
        if(input("imgs")){
            $imgArr = explode(",", input("imgs", ""));
            foreach ($imgArr as $value){
                $imgName = explode("http://admin.kireido.cn/interface/", $value);
                unlink("./".$imgName);
            }
        }
        $this->Code(200, "删除成功", []);
    }

}
